
import {Actions} from "react-native-router-flux";
import mockData from './mockBase64'
import apiPath from "../../utils/apiPath";
import fetch from "dva/fetch";
import {Alert,text} from 'react-native'

//长按短按
var timeCount = null;
let pressTime = 0;//按钮按下后计数
let pressUrl = '';
let pressType = '';

export default {
  namespace: 'sign',

  state: {
    signImgs: [],
    signInSignature: [],
    signOutSignature: [],
    SGSignature: [],
    JSSignature: [],
    JLSignature: [],
    JDSignature: [],
    KCSignature: [],
    SJSignature: [],
    JCSignature: [],
    imgUrl:'',
    thisOpen:false,
    thisAlertOpen:false,
    deleteImg:'',
    deleteType:''
  },

  subscriptions: {
  },

  effects: {
    *upload({payload}, {put, call, select}){

      const data = new FormData()

      data.append('file', {
        uri: 'file:///' + payload.uri,
        type: 'image/png',
        name: payload.name,
      })

      const url = yield fetch(GLOBAL.INT.imageUpload, {
        method: 'post',
        body: data
      }).then(res => {
        let data = JSON.parse(res._bodyInit).data
        return data.fileurl
      }).catch(err => {
        console.log(err)
        return null
      })
      let where=payload.signWordAction;
      if(url !== null){
        yield put({type: 'saveSuccess', payload: {url: url, type: payload.type}})
        // if(where=='SignIn'){
        //   Actions.push('SignIn')
        // }else if(where=='SignOut'){
        //   Actions.push('SignOut')
        // }else if(where=='RectifyBill'){
        //   Actions.push('RectifyBill')
        // }
        Actions.pop();
      }

    },

      //按下按钮开始计数
      *thisPressIn({payload},{put,call,select}){
          //开始计数，100ms计数一次
          pressUrl = payload.url;
          pressType = payload.signWordType;
          timeCount = setInterval(
              ()=>{
                  pressTime += 1;
              },
              100,
          );
      },
      //松开按钮获取按下后的计数值
      *thisPressOut({payload},{put,call,select}){
          timeCount && clearInterval(timeCount);//停止计数
          if(pressTime > 8){
              // 长按状态下的事件
              yield put({type:'cleanSignThis'})

          }else {
              yield put({type:'thisPhotoOpen'})
          }
          pressTime = 0;
      },


  },

  reducers: {
      cleanSignThis(state,{payload}){
          return {
              ...state,
              deleteImg:pressUrl,
              deleteType:pressType,
              thisAlertOpen:true
          }
      },
      thisAlertClose(state,{payload}){
          return {
              ...state,
              thisAlertOpen:false
          }
      },
    // 老版本保存签名
    // saveSuccess(state, {payload}){
    //   let type = payload.type
    //   state[type] = state[type].concat([{path: payload.path, base64: payload.base64}])
    //   return { ...state }
    // },
    saveSuccess(state, {payload}){
      let photoType = payload.type

      if (photoType === 'signInSignature') {
        state['signInSignature'] = state['signInSignature'].concat([{url:payload.url}])
      } else if (photoType === 'signOutSignature') {
        state['signOutSignature'] = state['signOutSignature'].concat([{url:payload.url}])
      } else if (photoType === 'SGSignature') {
        state['SGSignature'] = state['SGSignature'].concat([{url:payload.url}])
      } else if (photoType === 'JSSignature') {
        state['JSSignature'] = state['JSSignature'].concat([{url:payload.url}])
      } else if (photoType === 'JLSignature') {
        state['JLSignature'] = state['JLSignature'].concat([{url:payload.url}])
      }else if (photoType === 'JDSignature') {
        state['JDSignature'] = state['JDSignature'].concat([{url:payload.url}])
      }else if (photoType === 'KCSignature') {
        state['KCSignature'] = state['KCSignature'].concat([{url:payload.url}])
      }else if (photoType === 'SJSignature') {
        state['SJSignature'] = state['SJSignature'].concat([{url:payload.url}])
      }else if (photoType === 'JCSignature') {
        state['JCSignature'] = state['JCSignature'].concat([{url:payload.url}])
      }
      return { ...state }
    },

    //删除签字
    cleanSign(state,{payload}){
        let signImgs= []
        let signInSignature= state.signInSignature
        let signOutSignature= state.signOutSignature
        let SGSignature= state.SGSignature
        let JSSignature= state.JSSignature
        let JLSignature= state.JLSignature
        let JDSignature= state.JDSignature
        let KCSignature= state.KCSignature
        let SJSignature= state.SJSignature
        let JCSignature= state.JCSignature

        switch (pressType){
            case 'signInSignature':
                signImgs = state.signInSignature
                break
            case 'signOutSignature':
                signImgs = state.signOutSignature
                break
            case 'SGSignature':
                signImgs = state.SGSignature
                break
            case 'JSSignature':
                signImgs = state.JSSignature
                break
            case 'JLSignature':
                signImgs = state.JLSignature
                break
            case 'JDSignature':
                signImgs = state.JDSignature
                break
            case 'KCSignature':
              signImgs = state.KCSignature
              break
            case 'SJSignature':
              signImgs = state.SJSignature
              break
            case 'JCSignature':
              signImgs = state.JCSignature
              break
            default:break
        }

      if(signImgs !== undefined && signImgs != null){
        for(let i in signImgs){
          if(pressUrl === signImgs[i].url){
            signImgs.splice(i,1);
          }
        }
      }

      switch (pressType){
          case 'signInSignature':
              signInSignature = signImgs
              break
          case 'signOutSignature':
              signOutSignature = signImgs
              break
          case 'SGSignature':
              SGSignature = signImgs
              break
          case 'JSSignature':
              JSSignature = signImgs
              break
          case 'JLSignature':
              JLSignature = signImgs
              break
          case 'JDSignature':
              JDSignature = signImgs
              break
          case 'KCSignature':
            KCSignature = signImgs
            break
          case 'SJSignature':
            SJSignature = signImgs
            break
          case 'JCSignature':
            JCSignature = signImgs
            break
          default:break
      }

      return{...state,
        signInSignature:[].concat(signInSignature),
        signOutSignature:[].concat(signOutSignature),
        SGSignature:[].concat(SGSignature),
        JSSignature:[].concat(JSSignature),
        JLSignature:[].concat(JLSignature),
        JDSignature:[].concat(JDSignature),
        KCSignature:[].concat(KCSignature),
        SJSignature:[].concat(SJSignature),
        JCSignature:[].concat(JCSignature),
        thisAlertOpen:false
      }
    },

    //清空签名图片
    cleanSignImgs(state, {payload}){
      return {
        ...state,
        signImgs: [],
        signInSignature: [],
        signOutSignature: [],
        SGSignature: [],
        JSSignature: [],
        JLSignature: [],
        JDSignature: [],
        KCSignature: [],
        SJSignature: [],
        JCSignature: [],
        thisAlertOpen:false
      }
    },
      thisPhotoOpen(state,{payload}){
          return{...state,
              imgUrl:pressUrl,
              thisOpen:true
          }
      },
      thisPhotoClose(state,{payload}){
          return{...state,
              imgUrl:'',
              thisOpen:false
          }
      },
  },

}